//package com.wangyadong.hobby.excel.export;
//
//
//import com.wangyadong.hobby.excel.export.annotation.ExcelIDataFormatFiled;
//import com.wangyadong.hobby.excel.export.annotation.ExcelISheetFiled;
//import com.wangyadong.hobby.excel.export.annotation.ExcelIStyleFiled;
//import com.wangyadong.hobby.excel.export.annotation.ExcelImportFiled;
//import com.wangyadong.hobby.excel.export.demo.ExcelExportDemo;
//import javassist.*;
//import javassist.bytecode.AnnotationsAttribute;
//import javassist.bytecode.FieldInfo;
//import javassist.bytecode.annotation.*;
//import org.apache.commons.lang3.StringUtils;
//
//import org.apache.poi.ss.usermodel.HorizontalAlignment;
//import org.apache.poi.ss.usermodel.VerticalAlignment;
//import org.apache.poi.ss.util.CellRangeAddress;
//import org.apache.poi.xssf.usermodel.*;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.lang.reflect.Field;
//import java.lang.reflect.InvocationTargetException;
//import java.math.BigDecimal;
//import java.util.*;
//
//
//public class ExcelImportGenerate<T> {
//
//    private ExcelImportUtils utils;
//    private String sheetName;
//    private String className;
//    private XSSFWorkbook workBook;
//    private XSSFDataFormat xssfDataFormat;
//    private XSSFSheet sheet;
//    private final Map<String, Map<String, Object>> map = new HashMap();
//    private final Map<String, String> annotrationMap = new HashMap();
//
//
//    public ExcelImportGenerate(ExcelImportUtils utils) {
//        this.utils = utils;
//    }
//
//    public ExcelImportGenerate() {
//        this.utils = new ExcelImportUtils();
//
//    }
//
//    public ExcelImportGenerate updateAnnotation(String key, String value, String type) {
//        annotrationMap.put(key, value);
//        return this;
//    }
//
//
//    public ExcelImportGenerate setClassPool(Class clazz) {
//        this.utils.setClassPool(clazz);
//        return this;
//    }
//
//
//    /**
//     * @param className TestExcelVo.class.getName()
//     * @throws IOException
//     * @throws NotFoundException
//     */
//    public ExcelImportGenerate generateHead(String className) throws IOException, NotFoundException, NoSuchFieldException, IllegalAccessException {
//        CtClass ct = utils.javassistCtClass(className);
//        this.className = className;
//        workBook = new XSSFWorkbook();
//        this.xssfDataFormat = this.workBook.createDataFormat();
//        //创建工作表  工作表的名字叫helloWorld
//        Annotation annotation = utils.getAnnotation(className, ExcelISheetFiled.class.getName());
//        StringMemberValue sheetName = (StringMemberValue) annotation.getMemberValue("name");
//        this.setSheetName(sheetName.getValue());
//        sheet = workBook.createSheet(this.sheetName);
//        this.utils.getRowMap(sheet);
//        //遍历字段属性
//        CtField[] fields = ct.getDeclaredFields();
//        for (CtField f : fields) {
//            set(f, 1, null, null);
//        }
//
//        return this;
//    }
//
//    public ExcelImportGenerate<T> generateBody(List<T> list) throws IOException, NotFoundException, CannotCompileException
//            , IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//        int size = list.size();
//        //创建row,在创建多行
//        int startFillIndex = this.utils.getStartFillIndex();
//        for (int i = 0; i < size; i++) {
//            XSSFRow row = this.sheet.createRow(startFillIndex + i);
//            T t = list.get(i);
//            CtClass ctClass = this.utils.javassistCtClass(t.getClass().getName());
//            CtField[] declaredFields = ctClass.getDeclaredFields();
//            for (CtField field : declaredFields) {
//                set(field, 0, row, t);
//            }
//        }
////        workBook.write(new File("D://1111.xls"));
//        return this;
//    }
//
//
//    /**
//     * @param f     对应celld的字段
//     * @param type  1=生成head
//     * @param row1  对应execl的行
//     * @param clazz 对应cell字段的值 ，在generateHead为空
//     * @throws NoSuchFieldException
//     * @throws IllegalAccessException
//     */
//    private void set(CtField f, int type, XSSFRow row1, T clazz) throws NoSuchFieldException, IllegalAccessException {
//        FieldInfo fieldInfo = f.getFieldInfo();
//        AnnotationsAttribute attribute = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag);
//        if (attribute == null) {
//            return;
//        }
//        //获取表头信息 ，缺少列高
//        Annotation importFiled = attribute.getAnnotation(ExcelImportFiled.class.getName());
//        IntegerMemberValue startRow = (IntegerMemberValue) importFiled.getMemberValue("startRow");
//        IntegerMemberValue endRow = (IntegerMemberValue) importFiled.getMemberValue("endRow");
//        IntegerMemberValue startCell = (IntegerMemberValue) importFiled.getMemberValue("startCell");
//        IntegerMemberValue endCell = (IntegerMemberValue) importFiled.getMemberValue("endCell");
//        StringMemberValue title = (StringMemberValue) importFiled.getMemberValue("title");
//        BooleanMemberValue exclue = (BooleanMemberValue) importFiled.getMemberValue("exclue");
//        Integer integer = startRow.getValue();
//        XSSFRow row = this.utils.getRow(integer);
//        String titleValue = title.getValue();
//        if (type == 0) {
//            Field declaredField = clazz.getClass().getDeclaredField(f.getName());
//            declaredField.setAccessible(true);
//            Object value = declaredField.get(clazz);
//            if (value == null) {
//                return;
//            }
//            row = row1;
//            titleValue = (String) value;
//        }
//        if (type == 1) {
//            String annotationValue = this.annotrationMap.get(titleValue);
//            if (StringUtils.isNotBlank(annotationValue)) {
//                titleValue = annotationValue;
//            }
//
//        }
////        System.out.println("row num is " + row.getRowNum() + "  titleValue-- > " + titleValue + " cell num is " + startCell.getValue());
//
//        XSSFCell cell = row.createCell(startCell.getValue());
//        if (type == 1) {
//            if (exclue == null || exclue.getValue() == Boolean.TRUE) {
//                sheet.addMergedRegion(new CellRangeAddress(integer, endRow.getValue(),
//                        startCell.getValue(), endCell.getValue()));
//            }
//        }
//
//        //样式设置
//        XSSFCellStyle xssfCellStyle = updateExcelStyle(cell, row, attribute.getAnnotation(ExcelIStyleFiled.class.getName()));
//        cell.setCellValue(titleValue);
//        if (type == 0) {
//            Annotation dataformatFiled = attribute.getAnnotation(ExcelIDataFormatFiled.class.getName());
//            if (dataformatFiled != null) {
//                ShortMemberValue dataFormat = (ShortMemberValue) dataformatFiled.getMemberValue("dataFormat");
//                short format = this.xssfDataFormat.getFormat("0.00");
//                if (dataFormat != null) {
//                    setDataFormat(xssfCellStyle, format);
//                    BigDecimal value = null;
//                    if (StringUtils.isNotBlank(titleValue)) {
//                        if (titleValue instanceof String) {
//                            cell.setCellValue(new Double(titleValue));
//                        } else {
//                            cell.setCellValue(titleValue);
//                        }
//                    }
//
//
//                }
//            }
//        }
//
//    }
//
//    private void setDataFormat(XSSFCellStyle cellStyle, short dataFormat) {
//
//        cellStyle.setDataFormat(dataFormat);
//    }
//
//
//    private XSSFCellStyle updateExcelStyle(XSSFCell cell, XSSFRow row, Annotation style) {
//        XSSFCellStyle cellStyle = this.workBook.createCellStyle();
//
//        ShortMemberValue rowHight = (ShortMemberValue) style.getMemberValue("rowHight");
//        if (rowHight != null) {
//            row.setHeight(rowHight.getValue());
//        }
//        EnumMemberValue horizontalAlignment = (EnumMemberValue) style.getMemberValue("horizontalAlignment");
//        if (horizontalAlignment != null) {
////            cellStyle.setAlignment(HorizontalAlignment.valueOf(horizontalAlignment.getValue()));
//            cellStyle.setAlignment(HorizontalAlignment.CENTER);
//        }
//        EnumMemberValue verticalAlignment = (EnumMemberValue) style.getMemberValue("verticalAlignment");
//        if (verticalAlignment != null) {
////            VerticalAlignment verticalAlignment1 = VerticalAlignment.valueOf(verticalAlignment.getValue());
////            cellStyle.setVerticalAlignment(verticalAlignment1);
//            VerticalAlignment center = VerticalAlignment.CENTER;
//            cellStyle.setVerticalAlignment(center);
//
//        }
//        BooleanMemberValue booleanMemberValue = (BooleanMemberValue) style.getMemberValue("wrapText");
//        if (booleanMemberValue != null) {
//            cellStyle.setWrapText(booleanMemberValue.getValue());
//        }
//        IntegerMemberValue columnWidth = (IntegerMemberValue) style.getMemberValue("columnWidth");
//        if (columnWidth != null) {
//            //设置列宽度
//            int columnIndex = cell.getColumnIndex();
//            sheet.setColumnWidth(columnIndex, columnWidth.getValue() * 256);
//            sheet.autoSizeColumn(columnIndex, true);
//        }
//        cell.setCellStyle(cellStyle);
//        return cellStyle;
//    }
//
//    public ExcelImportUtils getUtils() {
//        return utils;
//    }
//
//    public void setUtils(ExcelImportUtils utils) {
//        this.utils = utils;
//    }
//
//    public String getSheetName() {
//        return sheetName;
//    }
//
//    public void setSheetName(String sheetName) {
//        this.sheetName = sheetName;
//    }
//
//    public String getClassName() {
//        return className;
//    }
//
//    public void setClassName(String className) {
//        this.className = className;
//    }
//
//    public XSSFWorkbook getWorkBook() {
//        return workBook;
//    }
//
//    public void setWorkBook(XSSFWorkbook workBook) {
//        this.workBook = workBook;
//    }
//
//    public XSSFDataFormat getXssfDataFormat() {
//        return xssfDataFormat;
//    }
//
//    public void setXssfDataFormat(XSSFDataFormat xssfDataFormat) {
//        this.xssfDataFormat = xssfDataFormat;
//    }
//
//    public XSSFSheet getSheet() {
//        return sheet;
//    }
//
//    public void setSheet(XSSFSheet sheet) {
//        this.sheet = sheet;
//    }
//
//    public static void main(String[] args) throws IOException, NotFoundException, InvocationTargetException, NoSuchMethodException, CannotCompileException, InstantiationException, IllegalAccessException, NoSuchFieldException {
//        ExcelImportGenerate generate = new ExcelImportGenerate();
//        List list = new ArrayList<>();
////        InvertDirgramView invertDirgramView = new InvertDirgramView();
////
////        invertDirgramView.setDate("2019-01-01");
////        invertDirgramView.setDeviceName("二元");
////        invertDirgramView.setAcPowerMax("234.234234234");
////        invertDirgramView.setPowerFactorAvg("1.3423423");
////        list.add(invertDirgramView);
//        ExcelImportGenerate generate1 = generate.generateHead(ExcelExportDemo.class.getName()).generateBody(list);
//        generate1.getWorkBook().write(new FileOutputStream(new File("C://data//11111.xlsx")));
//    }
//}
